import "package:mobx/mobx.dart";
import "package:hinge/common/models/topic_model.dart";
import "package:hinge/common/services/topic_service.dart";
import "package:hinge/common/services/topic_comment_service.dart";

part "message_store.g.dart";

class MessageStore = _MessageStore with _$MessageStore;

abstract class _MessageStore with Store {
  @observable
  bool isLoading = false;

  @observable
  ObservableList<String> messages = ObservableList<String>.of([]);

  @observable
  ObservableList<TopicModel> topics = ObservableList<TopicModel>.of([]);

  @action
  Future<void> loadMessages() async {
    isLoading = true;

    // 模拟网络请求
    await Future.delayed(const Duration(seconds: 1));

    // 加载消息数据
    messages.clear();
    messages.addAll(["消息1", "消息2", "消息3", "消息4", "消息5"]);

    isLoading = false;
  }

  @action
  Future<void> loadTopics() async {
    try {
      // 确保初始化默认话题和评论
      await TopicService.initDefaultTopics();
      await TopicCommentService.initDemoComments();

      // 获取所有话题
      final topicsList = TopicService.getAllTopics();

      // 更新observable列表
      topics.clear();
      topics.addAll(topicsList);

      print('已刷新话题列表，数量: ${topics.length}'); // 调试信息
    } catch (e) {
      print('加载话题数据出错: $e');
    }
  }
}
